package com.itlong.cloud.POJO.PO;

import com.itlong.cloud.POJO.DTO.property.CloudElevatorIotCallCtrlMessageDTO;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * <desc>
 *      呼梯指令delayQueue数据对象
 * </desc>
 *
 * @createDate 2018/04/12
 */
@Data
@NoArgsConstructor
public class SendCallElevatorMessageTaskPO implements Delayed{
    private String version;         //云电梯固件版本
    private String iotProductKey;  //iot 产品key
    private String deviceUnique;  //设备名称
    private String cmdVer;  //命令版本
    private String sourceFrom;  //命令发起标识
    private Map<String,Object> outData;     //外呼指令
    private Map<String,Object> inData;      //内呼指令
    private Integer callType;               //呼梯类型 对应枚举类 CloudElevatorCallTypeEnum
    private  long delay; //延迟时间
    private  long expire;  //到期时间
    private long snr;//序列号

    /**
     * <desc>
     *      初始化
     * </desc>
     *
     * @param version 云电梯固件版本
     * @param deviceUnique 设备名称
     * @param cmdVer 命令版本
     * @param sourceFrom 命令发起标识
     * @param outData 外呼指令
     * @param inData 内呼指令
     * @param delay 延迟时间
     * @author Jiaqi.X
     * @createDate 2018/04/12
     */
    public SendCallElevatorMessageTaskPO(String version,String iotProductKey,String deviceUnique, String cmdVer,
                                         String sourceFrom, Map<String, Object> outData,
                                         Map<String, Object> inData, long delay,Integer callType) {
        this.version = version;
        this.iotProductKey = iotProductKey;
        this.deviceUnique = deviceUnique;
        this.cmdVer = cmdVer;
        this.sourceFrom = sourceFrom;
        this.outData = outData;
        this.inData = inData;
        this.delay = delay;
        this.callType = callType;
        this.expire = System.currentTimeMillis() + delay;
    }

    /**
     * <desc>
     *      延迟时间
     * </desc>
     *
     * @param unit
     * @return
     * @author Jiaqi.X
     * @createDate 2018/04/12
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expire - System.currentTimeMillis() , TimeUnit.MILLISECONDS);
    }

    /**
     * <desc>
     *      用于延迟队列内部比较排序   当前时间的延迟时间 - 比较对象的延迟时间
     * </desc>
     *
     * @param o
     * @return
     * @author Jiaqi.X
     * @createDate 2018/04/12
     */
    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) -o.getDelay(TimeUnit.MILLISECONDS));
    }
}
